﻿using QLDVu.BO;
using QLKetNoi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLDVu.GUI
{
    public partial class QuanLyPhieuSuaForm : Form
    {
        public static string masuachua;
        public QuanLyPhieuSuaForm()
        {
            InitializeComponent();
        }
        int SelectedSuaChuaIndex = 0;
        private void btnSua_Click(object sender, EventArgs e)
        {
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnLuu.Enabled = true;
            btnLuu.Text = "Lưu";
            EnableControl(true);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();
            SUACHUA SuaChua = SuaChuaBO.LayThongTinPhieuSuaTheoMaBaoGia(txtSoBaoGia.Text);
            string MaPhieuSua = txtMaPhieuSua.Text;
            SuaChua.NgayHoanThanh = dtpNgayHoanThanh.Value;
            SuaChua.GhiChu = txtGhiChu.Text;
            SuaChua.TinhTrang = cbTinhTrang.Text;

            if (txtKmRa.Text != "" && txtKmVao.Text != "")
                SuaChuaBO.SuaThongTinPhieuSua(MaPhieuSua, dtpNgayVao.Value, cbTinhTrang.SelectedItem.ToString(), Convert.ToInt32(txtKmVao.Text), dtpNgayHoanThanh.Value, Convert.ToInt32(txtKmRa.Text), txtGhiChu.Text);
            else if (txtKmVao.Text != "")
                SuaChuaBO.SuaThongTinPhieuSua(MaPhieuSua, dtpNgayVao.Value, cbTinhTrang.SelectedItem.ToString(), Convert.ToInt32(txtKmVao.Text), dtpNgayHoanThanh.Value, txtGhiChu.Text);
            else if (txtKmRa.Text != "")
                SuaChuaBO.SuaThongTinPhieuSua(MaPhieuSua, dtpNgayVao.Value, cbTinhTrang.SelectedItem.ToString(), dtpNgayHoanThanh.Value, Convert.ToInt32(txtKmRa.Text), txtGhiChu.Text);
            else
                SuaChuaBO.SuaThongTinPhieuSua(MaPhieuSua, dtpNgayVao.Value, cbTinhTrang.SelectedItem.ToString(), dtpNgayHoanThanh.Value, txtGhiChu.Text);

            SuaChiTietPhieuSua(); //Sua thong tin chi tiet phieu sua
            MessageBox.Show("Đã cập nhật thông tin phiếu sữa");
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnLuu.Enabled = false;
            EnableControl(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn muốn xóa ?", "Xác nhận", MessageBoxButtons.YesNo);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                //Xoa Chi tiet Phieu sua truoc
                XoaChiTietPhieuSua(txtMaPhieuSua.Text);

                //Sau do xoa Phieu sua
                PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();
                SUACHUA SuaChua = SuaChuaBO.LayThongTinPhieuSuaTheoMaPhieuSua(txtMaPhieuSua.Text);
                SuaChuaBO.XoaPhieuSuaChua(SuaChua.MaSuaChua);
                MessageBox.Show("Xóa thành công");

                //Reload dgvDanhSachPhieuSua
                dgvDanhSachPhieuSua.DataSource = SuaChuaBO.LayDanhSachPhieuSua();
            }
            else
            {
                return;
            }
        }

        private void btnInPhieuSua_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> ThongTinSuaChua = new Dictionary<string, string>();

            //Thong tin khach
            ThongTinSuaChua.Add("TenKhachHang", txtTenKhach.Text.Trim());
            ThongTinSuaChua.Add("DiaChi", txtDiaChi.Text.Trim());

            //Thong tin phieu sua
            ThongTinSuaChua.Add("MaSuaChua", txtMaPhieuSua.Text.Trim());
            ThongTinSuaChua.Add("NgayVao", dtpNgayVao.Value.ToShortDateString());
            if (dtpNgayHoanThanh.Text.Trim() != "")
                ThongTinSuaChua.Add("NgayRa", dtpNgayHoanThanh.Value.ToShortDateString());
            else
                ThongTinSuaChua.Add("NgayRa", dtpNgayHoanThanh.Text.Trim());
            ThongTinSuaChua.Add("SoKmVao", txtKmVao.Text.Trim());
            ThongTinSuaChua.Add("SoKmRa", txtKmRa.Text.Trim());
            ThongTinSuaChua.Add("GhiChu", txtGhiChu.Text.Trim());

            //Thong tin xe
            PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();
            SuaChuaBO.LayThongTinXe(dgvDanhSachPhieuSua.Rows[SelectedSuaChuaIndex].Cells[1].Value.ToString(), ref ThongTinSuaChua);

            RPSuaChua form = new RPSuaChua(ThongTinSuaChua);
            form.Show();
        }

        private void QuanLyPhieuSuaForm_Load(object sender, EventArgs e)
        {
            //Load danh sach phieu sua chua
            PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();
            dgvDanhSachPhieuSua.DataSource = SuaChuaBO.LayDanhSachPhieuSua();

            //tao DataGridViewCombobox TenNhanVien
            var comboNV = new DataGridViewComboBoxColumn();
            comboNV.HeaderText = "Nhân viên thực hiện";
            comboNV.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            comboNV.DataSource = SuaChuaBO.LayDanhSachNhanVien();
            dgvChiTietSuaChua.Columns.Insert(2, comboNV);
        }

        private void dgvChiTietSuaChua_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            SelectedSuaChuaIndex = e.RowIndex;
            string MaPhieuSua = dgvDanhSachPhieuSua.Rows[e.RowIndex].Cells[0].Value.ToString();

            //Load thong tin phieu sua
            PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();
            SUACHUA SuaChua = SuaChuaBO.LayThongTinPhieuSuaTheoMaPhieuSua(MaPhieuSua);
            txtMaPhieuSua.Text = SuaChua.MaSuaChua;
            dtpNgayVao.Value = SuaChua.NgayVao;
            dtpNgayHoanThanh.Value = (DateTime)SuaChua.NgayHoanThanh;
            txtKmVao.Text = SuaChua.SoKmVao.ToString();
            txtKmRa.Text = SuaChua.SoKmRa.ToString();
            txtGhiChu.Text = SuaChua.GhiChu;
            cbTinhTrang.Text = SuaChua.TinhTrang;

            //Load thong tin phieu bao gia
            txtSoBaoGia.Text = SuaChua.BAOGIA.MaBaoGia;
            txtNgayLap.Text = SuaChua.BAOGIA.NgayLap.ToShortDateString();
            txtDienThoai.Text = SuaChua.BAOGIA.KHACHHANG.DienThoai;
            txtDiaChi.Text = SuaChua.BAOGIA.KHACHHANG.DiaChi;
            txtEmail.Text = SuaChua.BAOGIA.KHACHHANG.Email;
            txtBienSo.Text = SuaChua.BAOGIA.XE.BienSoXe;
            txtTenKhach.Text = SuaChua.BAOGIA.KHACHHANG.TenKhachHang;

            //Load thong tin chi tiet phieu sua chua
            dgvChiTietSuaChua.Rows.Clear();
            List<string[]> ThongTinCTSC = new List<string[]>();
            ChiTietSuaChuaBO ChiTietSuaBO = new ChiTietSuaChuaBO();
            ThongTinCTSC = ChiTietSuaBO.LayThongTinChiTietSuaChua(txtMaPhieuSua.Text);
            foreach (var s in ThongTinCTSC)
            {
                dgvChiTietSuaChua.Rows.Add(s);
            }    
        }

        private void txtBienSo_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void txtBienSoTK_TextChanged(object sender, EventArgs e)
        {
            PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();
            if (txtBienSoTK.Text.Trim() != "")
            {
                dgvDanhSachPhieuSua.DataSource = SuaChuaBO.LayDanhSachPhieuSua(txtBienSoTK.Text);
            }
            else
            {
                dgvDanhSachPhieuSua.DataSource = SuaChuaBO.LayDanhSachPhieuSua();
            }
        }
        private void SuaChiTietPhieuSua()
        {
            ChiTietSuaChuaBO ChiTietSuaBO = new ChiTietSuaChuaBO();
            for (int i = 0; i < dgvChiTietSuaChua.RowCount; i++)
            {
                string TenNguoiDung = null;
                string MaChiTietSC = dgvChiTietSuaChua.Rows[i].Cells[0].Value.ToString();
                if (dgvChiTietSuaChua.Rows[i].Cells[2].Value != null)
                    TenNguoiDung = dgvChiTietSuaChua.Rows[i].Cells[2].Value.ToString();
                ChiTietSuaBO.SuaChiTietPhieuSua(MaChiTietSC, TenNguoiDung);
            }
        }
        private void XoaChiTietPhieuSua(string MaPhieuSua)
        {
            ChiTietSuaChuaBO ChiTietSuaBO = new ChiTietSuaChuaBO();

            ChiTietSuaBO.XoaChiTietPhieuSuaTuMaPhieuSua(MaPhieuSua);
        }


        private void EnableControl(bool value)
        {
            dtpNgayVao.Enabled = value;
            dtpNgayHoanThanh.Enabled = value;
            txtKmVao.Enabled = value;
            txtKmRa.Enabled = value;
            txtGhiChu.Enabled = value;
            dgvChiTietSuaChua.Enabled = value;
            cbTinhTrang.Enabled = value;
        }

        private void dgvChiTietSuaChua_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewComboBoxEditingControl)
            {
                ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
                ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
                ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
            }
        }

        private void dgvDanhSachPhieuSua_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            SelectedSuaChuaIndex = e.RowIndex;
            string MaPhieuSua = dgvDanhSachPhieuSua.Rows[e.RowIndex].Cells[0].Value.ToString();

            //Load thong tin phieu sua
            PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();
            SUACHUA SuaChua = SuaChuaBO.LayThongTinPhieuSuaTheoMaPhieuSua(MaPhieuSua);
            txtMaPhieuSua.Text = SuaChua.MaSuaChua;
            dtpNgayVao.Value = SuaChua.NgayVao;
            dtpNgayHoanThanh.Value = (DateTime)SuaChua.NgayHoanThanh;
            txtKmVao.Text = SuaChua.SoKmVao.ToString();
            txtKmRa.Text = SuaChua.SoKmRa.ToString();
            txtGhiChu.Text = SuaChua.GhiChu;
            cbTinhTrang.Text = SuaChua.TinhTrang;

            //Load thong tin phieu bao gia
            txtSoBaoGia.Text = SuaChua.BAOGIA.MaBaoGia;
            txtNgayLap.Text = SuaChua.BAOGIA.NgayLap.ToShortDateString();
            txtDienThoai.Text = SuaChua.BAOGIA.KHACHHANG.DienThoai;
            txtDiaChi.Text = SuaChua.BAOGIA.KHACHHANG.DiaChi;
            txtEmail.Text = SuaChua.BAOGIA.KHACHHANG.Email;
            txtBienSo.Text = SuaChua.BAOGIA.XE.BienSoXe;
            txtTenKhach.Text = SuaChua.BAOGIA.KHACHHANG.TenKhachHang;

            //Load thong tin chi tiet phieu sua chua
            dgvChiTietSuaChua.Rows.Clear();
            List<string[]> ThongTinCTSC = new List<string[]>();
            ChiTietSuaChuaBO ChiTietSuaBO = new ChiTietSuaChuaBO();
            ThongTinCTSC = ChiTietSuaBO.LayThongTinChiTietSuaChua(txtMaPhieuSua.Text);
            foreach (var s in ThongTinCTSC)
            {
                dgvChiTietSuaChua.Rows.Add(s);
            }    
        }

       

    }
}
